/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nhncorp.neptune;

import java.io.IOException;

import com.nhncorp.neptune.common.conf.NConfiguration;
import com.nhncorp.neptune.neptune_test.common.MiniNeptuneCluster;
import com.nhncorp.neptune.neptune_test.common.MiniPipeBasedChangeLogCluster;
import com.nhncorp.pleiades.conf.PleiadesConfiguration;
import com.nhncorp.pleiades.ens.EventManager;
import com.nhncorp.pleiades.master.ClusterManager;

/**
 * Sinlge process에 클러스터에 참여하는 모든 데몬을 실행시켜 개발용이나 로컬에서 데이터 모델에 대한 검증 등
 * 간단한 테스트를 수행하는데 활용
 * @author jindolk
 *
 */
public class LocalCluster {
  public void startCluster(String dataDir) throws Exception {
    PleiadesConfiguration pconf = new PleiadesConfiguration();
    if(dataDir != null) {
      pconf.set("pleiades.root", dataDir + "/pleiades");
    }

    new MiniPleiadesCluster(pconf);
    System.out.println("======================= Pleiades Started ==============================");
    Thread.sleep(5000);
    
    NConfiguration nconf = new NConfiguration();
    nconf.set("pleiades.service.name", "neptune");
    nconf.set("neptune.filesystem", "local");
    if(dataDir != null) {
      nconf.set("neptune.root", dataDir);
      nconf.set("neptune.local.temp", dataDir + "/localtemp");
      nconf.set("changelog.data.dir", dataDir + "/changelog/data");
      nconf.set("changelog.image.dir", dataDir + "/changelog/image");
    }
    
    new MiniPipeBasedChangeLogCluster(nconf);
    System.out.println("=================== Changelog Server Started ==========================");
    Thread.sleep(5000);

    new MiniNeptuneCluster(nconf, 1, 3, false, null);
    System.out.println("================== Complete starting LocalCluster ======================");
  }
  
  public static void main(String[] args) throws Exception {
    String dataDir = null;
    if(args.length > 0) {
      dataDir = args[0];
    }
    
    (new LocalCluster()).startCluster(dataDir);
  }
  
  class MiniPleiadesCluster {
    ClusterManager clusterManager;
    EventManager eventManager;
    
    public MiniPleiadesCluster(PleiadesConfiguration pconf) throws Exception {
      ClusterManager clusterManager = new ClusterManager("conf/session.prop", pconf);
      (new Thread(clusterManager)).start();
      
      EventManager eventManager = new EventManager(pconf);
      (new Thread(eventManager)).start();
    }
  }
}
